package com.example.windowdemo.window.single;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.LayoutRes;

/**
 * @author: njb
 * @date: 2023/8/1 0:07
 * @desc:
 */
public class WindowSingleBuilder {
    private boolean isFullScreen = false;
    private boolean hasTitleBar = true;
    private int width = WindowManager.LayoutParams.MATCH_PARENT;
    private int height = WindowManager.LayoutParams.WRAP_CONTENT;
    private int x = 0;
    private int y = 0;
    private int gravity = Gravity.CENTER;
    private View contentView;
    private Context context;

    public WindowSingleBuilder(Context context){
        this.context = context;
    }

    public WindowSingleBuilder setFullScreen(boolean isFullScreen,Context context) {
        this.isFullScreen = isFullScreen;
        return this;
    }

    public WindowSingleBuilder setTitleBar(boolean hasTitleBar) {
        this.hasTitleBar = hasTitleBar;
        return this;
    }

    public WindowSingleBuilder setSize(int width, int height) {
        this.width = width;
        this.height = height;
        return this;
    }

    public WindowSingleBuilder setPosition(int x, int y) {
        this.x = x;
        this.y = y;
        return this;
    }

    public WindowSingleBuilder setGravity(int gravity) {
        this.gravity = gravity;
        return this;
    }

    public WindowSingleBuilder setContentView(@LayoutRes int layoutResId) {
        this.contentView = LayoutInflater.from(context).inflate(layoutResId, null);
        return this;
    }

    public WindowSingleBuilder setContentView(View view) {
        this.contentView = view;
        return this;
    }

    public WindowSingle build(Context context) {
        WindowSingle window = WindowSingle.getInstance(context);
        window.setFullScreen(isFullScreen);
        window.setTitleBar(hasTitleBar);
        window.setSize(width, height);
        window.setPosition(x, y);
        window.setGravity(gravity);
        window.addView(contentView);
        return window;
    }
}
